<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('出库单据列表')"/>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.css"/>

</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>外向交货单：</label>
                            <input type="text" name="stockoutGuid"/>
                        </li>
                        <!--                            <li>-->
                        <!--                                <label>出库单编号：</label>-->
                        <!--                                <input type="text" name="stockoutId"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>出库类型：</label>-->
                        <!--                                <select id="outType" name="outType" required></select>-->
                        <!--                            </li>-->

                        <li>
                            <label>单据状态：</label>
                            <select id="status" name="status" th:height="300px" onchange="$.table.search()">
                                <!--<option value="">-&#45;&#45;请选择-&#45;&#45;</option>-->
                                <option value="1" selected="selected">出库开单</option>
                                <option value="2">已审核</option>
                                <option value="3">单据执行</option>
                                <option value="4">执行完成</option>
                                <option value="5">单据确认</option>
                                <option value="6">单据作废</option>
                                <option value="7">回传完成</option>
                            </select>
                        </li>
                        <!--                            <li>-->
                        <!--                                <label>单据来源：</label>-->
                        <!--                                <input type="text" name="stockoutSource"/>-->
                        <!--                            </li>-->

                        <!--                            <li>-->
                        <!--                                <label>出口：</label>-->
                        <!--                                <input type="text" name="exitId"/>-->
                        <!--                            </li>-->
                        <li>
                            <label>建单时间：</label><input type="text" placeholder="请单击选择开始时间："
                                                           name="beginDate"
                                                           id="beginDate"/>---
                            <input type="text" placeholder="请单击选择结束时间：" name="finishDate" id="finishDate"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                        <li>
                            <select class="selectpicker" id="exit" title="请选择出库口">
                                <option value="0">---请选择出口---</option>
                                <option value="1">---1号出口---</option>
                                <option value="2">---2号出口---</option>
                                <option value="3">---3号出口---</option>
                                <option value="4">---4号出口---</option>
                                <option value="5">---5号出口---</option>
                                <option value="6">---6号出口---</option>
                            </select>

                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <!--                <a class="btn btn-success" onclick="updateStatus(2)">-->
            <!--                    <i class="fa fa-plus"></i> 单据审核-->
            <!--                </a>-->
            <a class="btn btn-primary " onclick="updateStatus(5)" shiro:hasPermission="mydo:stockoutmanage:reciptSure">
                <i class="fa fa-edit"></i> 单据确认
            </a>
            <a class="btn btn-danger " onclick="updateStatus(6)"
               shiro:hasPermission="mydo:stockoutmanage:receiptCancle">
                <i class="fa fa-remove"></i> 单据作废
            </a>
            <a class="btn btn-warning" onclick="autoAllocation()"
               shiro:hasPermission="mydo:stockoutmanage:autoAllocation">
                <i class="fa fa-download"></i> 自动分配
            </a>
            <a class="btn btn-primary" onclick="updateStatus(2)"
               shiro:hasPermission="mydo:stockoutmanage:receiptCancle">
                <i class="fa fa-download"></i> 单据审核
            </a>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


            <!--                <a class="btn btn-warning" onclick="handleAllocation()"shiro:hasPermission="mydo:stockoutmanage:handleAllocation" >-->
            <!--                    <i class="fa fa-download"></i> 手动分配-->
            <!--                </a>-->
        </div>

        <div id="div1" style="height:350px" class="col-sm-8 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
        <div id="div2" style="height:350px" class="col-sm-8 select-table table-striped">
            <table id="bootstrap-table2" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script src="/js/bootstrap-select.js" type="text/javascript"></script>
<script th:inline="javascript">

    var editFlag = [[${@permission.hasPermi('stockout:stockout:edit')}]];
    var removeFlag = [[${@permission.hasPermi('stockout:stockout:remove')}]];
    var prefix = ctx + "stockout/stockout";
    layui.use('laydate', function () {
        var laydates = layui.laydate;
        laydates.render({
            elem: '#beginDate'
            , type: 'datetime',
            /* ,range: '到' */
            format: 'yyyy-MM-dd HH:mm:ss'
        })
    })

    layui.use('laydate', function () {
        var laydates = layui.laydate;
        laydates.render({
            elem: '#finishDate'
            , type: 'datetime',
            /* ,range: '到' */
            format: 'yyyy-MM-dd HH:mm:ss'
        })
    })
    //出库单据类型
    $(document).ready(function () {
        $.post("/bas/basType/list", {parentId: '04'}, function (data) {

            $("#outType").append("<option value=''>全部</option>");
            // alert(data.rows[0].inOrOut);
            for (var i = 0; i < data.total; i++) {
                $("#outType").append("<option value=" + data.rows[i].typeValue + ">" + data.rows[i].typeName + "</option>");
            }
        });
    });


    //单据来源
    $(document).ready(function () {
        $.post("/bas/basType/list", {parentId: '08'}, function (data) {

            $("#stockoutSource").append("<option value='' selected>全部</option>");
            // alert(data.rows[0].inOrOut);
            for (var i = 0; i < data.total; i++) {
                $("#stockoutSource").append("<option value=" + data.rows[i].typeValue + ">" + data.rows[i].typeName + "</option>");
            }
        });
    });

    //子表
    var options2 = {

        search: false,
        showRefresh: true,
        showColumns: true,
        minimumCountColumns: 2,
        clickToSelect: true,//是否启用点击选中行
        // height: $("#div2").height() / 1,
        sortName: "assignNum",
        sortOrder: "desc",
        modalName: "入库单据子表",
        method: 'post',
        // singleSelect:true,
        striped: true, //是否显示行间隔色
        pagination: true,//显示分页
        sidePagination: "client",
        pageNumber: 1,
        pageSize: 15,
        pageList: [10, 25],
        columns:
            [{
                checkbox: true
            },
                {
                    field: 'stockoutDetailId',
                    title: '出库子单据号'
                },
                {
                    field: 'stockoutGuid',
                    title: '出库单guid',
                    visible: false
                },
                {
                    field: 'stockoutId',
                    title: '出库单据号'
                },
                {
                    field: 'lotNo',
                    title: '批号',
                    visible: false
                },
                // {
                //     field: 'productGuid',
                //     title: '物品guid',
                //     visible: false
                // },
                {
                    field: 'productId',
                    title: '物料子件编号'
                },
                {
                    field: 'productGuid',
                    title: '物料编号'
                },
                // {
                //     field: 'productName',
                //     title: '物料名称'
                // },
                {
                    field: 'remark',
                    title: '单位'
                },
                // {
                //     field: 'supplierName',
                //     title: '供应商'
                // },
                {
                    field: 'quantity',
                    title: '计划数量'
                },
                {
                    field: 'assignNum',
                    title: '实际数量'
                },
                {
                    field: 'allotQuantity',
                    title: '分配数量'
                },
                // {
                //     field: 'unitName',
                //     title: '包装定额'
                // },
                // {
                //     field: 'entryid',
                //     title: '行号'
                // },
                // {
                //     field: 'fid',
                //     title: '富晟明细编号'
                // },
                // {
                //     field: 'erpStockoutId',
                //     title: '富晟单据编号'
                // },
                // {
                //     field: 'taskCode',
                //     title: '富晟任务号'
                // },


            ],
        onLoadSuccess(data) {
            switch (data.code) {
                case  500:
                    options2.url = "";
                    $('#bootstrap-table2').bootstrapTable("removeAll")
                    $.modal.alertWarning("查询出错！");
                    break;
                default:
                    break;
            }
        },
    }
    $('#bootstrap-table2').bootstrapTable(options2)

    //主表
    var options = {
        url: prefix + "/list",
        detailView: false,//父子表
        search: false,
        showRefresh: true,
        showColumns: true,
        minimumCountColumns: 2,
        singleSelect: true,
        clickToSelect: true,//是否启用点击选中行
        height: $("#div1").height() / 1,
        sortName: "createDate",
        sortOrder: "desc",
        modalName: "出库单据",
        columns: [{
            checkbox: true
        },
            {
                field: 'stockoutGuid',
                title: '交货单号'
            },
            {
                field: 'stockoutId',
                title: '出库单编号'
            },
            {
                field: 'outType',
                title: '出库类型',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "销售出库";
                    } else if (value == 2) {
                        return "配套出库";
                    }
                }
            },
            {
                field: 'createDate',
                title: '建单日期'
            },
            // {
            //     field: 'departmentNo',
            //     title: '部门编号'
            // },
            {
                field: 'customerNo',
                title: '客户编号'
            },
            {
                field: 'createManId',
                title: '建单人'
            },
            {
                field: 'status',
                title: '单据状态',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "入库开单";
                    } else if (value == 2) {
                        return "已审核";
                    } else if (value == 3) {
                        return "单据执行";
                    } else if (value == 4) {
                        return "执行完成";
                    } else if (value == 5) {
                        return "单据确认";
                    } else if (value == 6) {
                        return "单据作废";
                    } else if (value == 7) {
                        return "回传完成";
                    }

                }
            }
            // {
            //     field: 'stockoutSource',
            //     title: '单据来源'
            // },
            // {
            //     field: 'warehouseId',
            //     title: '仓库编号',
            //     formatter: function (value, row, index) {
            //         if (value == '01') {
            //             return "立库";
            //         } else if (value == '02') {
            //             return "平库";
            //         }
            //     }
            // },
            // {
            //     field: 'classNowId',
            //     title: '班组编号'
            // },
            // {
            //     field: 'exitId',
            //     title: '出口'
            // },
            // {
            //     field: 'carNumber',
            //     title: '车牌号'
            // },
            // {
            //     field: 'beginDate',
            //     title: '开始日期'
            // },
            // {
            //     field: 'finishDate',
            //     title: '完成日期'
            // },
            // {
            //     field: 'FID',
            //     title: 'fid'
            // },
            // {
            //     field: 'erpStockoutId',
            //     title: '交货单行号'
            // }
            // {
            //     title: '操作',
            //     align: 'center',
            //     formatter: function (value, row, index) {
            //         var actions = [];
            //         actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.stockoutGuid + '\')"><i class="fa fa-edit"></i>编辑</a> ');
            //         actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.stockoutGuid + '\')"><i class="fa fa-remove"></i>删除</a>');
            //         return actions.join('');
            //     }
            // }
        ],
        //双击显示子表
        onClickRow: function (t, e) {
            console.log(1111)
            // initSubTable(index, row, $detail);
            options2.url = ctx + "stockoutdetail/stockoutdetail/list?stockoutId=" + t.stockoutId + "&status=" + t.status;
            $('#bootstrap-table2').bootstrapTable("refresh", options2)
        },
        onLoadSuccess(data) {
            switch (data.code) {
                case  500:
                    options2.url = "";
                    $('#bootstrap-table').bootstrapTable("removeAll");
                    $('#bootstrap-table2').bootstrapTable("removeAll");
                    $.modal.alertWarning("查询出错！");
                    break;
                default:
                    $('#bootstrap-table2').bootstrapTable("removeAll");
                    break;
            }
        },
    };
    $.table.init(options);


    //单据审核，单据确认，单据审核
    function updateStatus(newStatusValue) {
        var exit = $("#exit").val();

        var row = $("#bootstrap-table").bootstrapTable('getSelections');
        if (row.length == 0) {
            $.modal.alertWarning("请至少选择一条数据")
        }
        var exitId = $("#exitId").val();
        if (newStatusValue == 2 && exitId == "") {
            $.modal.alertWarning("请输入出口")
            return;
        }

        var stockoutIds = [];
        for (var i = 0; i < row.length; i++) {

            stockoutIds.push(row[i].stockoutId);
        }
        var idParam = {};
        idParam.stockoutIds = stockoutIds;
        idParam.status = row[0].status;
        idParam.newStatus = newStatusValue;
        idParam.exit = exit;

        $.ajax({
            "url": "/stockout/stockout/updateStatus",
            "type": "POST",
            "dataType": "json",
            "data": idParam,
            "success": function (result) {
                console.log(result);
                $.modal.alertWarning(result.errorInfo);
                $.table.search();
                // alert(result.errorInfo);
                // window.location.reload();

            }
        });
    }


    //自动分配
    function autoAllocation() {
        var exit = $("#exit").val();
        if (exit == 0) {
            $.modal.alertWarning("请选择出库口，再进行分配！")
            return;
        }
        var row = $("#bootstrap-table").bootstrapTable('getSelections');
        if (row.length == 0) {
            $.modal.alertWarning("请至少选择一条数据")
            return;
        }

        var stockoutIds = [];
        for (var i = 0; i < row.length; i++) {
            console.log(row[i].status);
            // if(row[i].status!=2&&row[i].status!=3){
            //     $.modal.alertWarning("单据未审核！")
            //     return;
            // }
            stockoutIds.push(row[i].stockoutId)
        }
        var params = {};
        params.stockoutIds = stockoutIds;
        params.exit = exit;
        $.ajax({
            "url": "/stockout/stockout/autoAllocation",
            "type": "POST",
            "dataType": "json",
            "data": params,
            "success": function (result) {
                console.log(result);
                $.modal.alertWarning(result.errorInfo);
                $.table.search();
            }
        });
    }

    //手动分配
    function handleAllocation() {
        var row1 = $("#bootstrap-table").bootstrapTable('getSelections');
        if (row1.length != 1) {
            $.modal.alertWarning("请选择一条主数据");
            return;
        }
        var row = $("#bootstrap-table2").bootstrapTable('getSelections');
        if (row.length != 1) {
            $.modal.alertWarning("请选择一条子数据");
            return;
        }
        console.log(row);
        var productId = row[0].productId;
        var warehouseId = row1[0].warehouseId;
        var stockoutDetailId = row[0].stockoutDetailId;//出库子单号
        var options = {
            title: '选择母件',
            url: "/stockout/stockout/toStockMain?productId=" + productId + "&stockoutDetailId=" + stockoutDetailId + "&warehouseId" + warehouseId,
            callBack: doSubmit2
        };
        $.modal.openOptions(options);
    }

    function doSubmit2(index, layero) {
        var rows = layero.find("iframe")[0].contentWindow.getSelections();//获取到行主键
        var stockoutDetailId = layero.find("iframe")[0].contentWindow.getDetailId();//获取到子单据号
        if (rows.length != 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }
        // console.log(rows);
        //console.log(stockoutDetailId);

        $.ajax({
            url: "/stockout/stockout/handleAllocation",
            type: "post",
            data: {"stockoutDetailId": stockoutDetailId, "stockMainId": rows.join()},
            success: function (result) {
                //console.log(result);
                $.modal.alertSuccess(result.msg);
            },
            error: function () {
                $.modal.alertError("系统异常稍后重试");
            }
        });
        $.modal.close(index);
    }
</script>
</body>
</html>